//
//  ChengJiViewController.m
//  Yu01
//
//  Created by 蒋松涵 on 16/8/25.
//  Copyright © 2016年 蒋松涵. All rights reserved.
//

#import "ChengJiViewController.h"
#import "model.h"
#import "EvaluationViewController.h"

@interface ChengJiViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_sectionTitleArr;
    NSMutableArray *_allSamllArr;
    
    BOOL _flagArr[3];
    
    int _select;

}
@property (nonatomic,strong)UITableView * tableView;

@property(nonatomic,strong)NSMutableArray * dataArray;
@property (nonatomic,retain) NSString *urlStr;
@property (nonatomic,retain) NSDictionary *urlParameters;


@end

@implementation ChengJiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的成绩";
    self.view.backgroundColor = [UIColor colorWithRed:232/255.0 green:234/255.0 blue:233/255.0 alpha:1];
    
    _urlStr = @"http://www.100xsys.com.cn/school/app/studentScoreInfo/listJson";
    _urlParameters = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"appWebConfigId",@"1",@"page",@"10",@"rows",@"1",@"studentScoreInfo.studentInfoId",@"2015",@"studentScoreInfo.year",@"2",@"studentScoreInfo.termNumber", nil];
    
    [self fecthDataFromNet:_urlStr parameters:_urlParameters];
    
    
    
    [self init_View];
    [self tableview];
}
#pragma mark - 数据请求
//请求数据
- (void)fecthDataFromNet:(NSString *)url parameters:(NSDictionary *)parameter {
    
    NSLog(@"%@",url);
    NSLog(@"%@",parameter);
    //初始化请求工具
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
        NSLog(@"tttttt%@",dic);
        NSMutableArray * List = dic[@"rows"];
        for (NSDictionary * dicList in List)
        {
            model * Model = [[model alloc]init];
            [Model setValuesForKeysWithDictionary:dicList];
            [self.dataArray addObject:Model];
            NSLog(@"--===-%ld",self.dataArray.count);
        }
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        
    }];
}

#pragma mark - View_
-(void)init_View
{
    CGFloat h = 736 / ScreenHeight;
    CGFloat w = 414 / ScreenWidth;
    
    UIView * Vi_s = [[UIView alloc]initWithFrame:CGRectMake(0, 70/h, ScreenWidth, 50/h)];
    Vi_s.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:Vi_s];
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(20/w, 6/h, 120/w, 30/h)];
    label.text = @"总学分:";
    [Vi_s addSubview:label];
    
    
    UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(100/w, 6/h, 120/w, 30/h)];
    label1.text = @"已修学分4";
    [Vi_s addSubview:label1];
    
    UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(220/w, 6/h, 120/w, 30/h)];
    label2.text = @"应修学分16";
    [Vi_s addSubview:label2];
    
}

#pragma mark - tableView
-(void)tableview
{
    CGFloat h = 736 / ScreenHeight;
//    CGFloat w = 414 / ScreenWidth;
    
    
    _sectionTitleArr = [[NSMutableArray alloc] initWithObjects:@"2015-2016学年 第2学期",@"2015-2016学年 第1学期",@"2014-2015学年 第2学期", nil];
    NSMutableArray *familyArr = [[NSMutableArray alloc] initWithObjects:@"平面设计",@"大学英语",@"思想政治",@"色彩", nil];
    NSMutableArray *friendArr = [[NSMutableArray alloc] initWithObjects:@"平面设计",@"大学英语",@"思想政治",@"色彩", nil];
    NSMutableArray *classArr = [[NSMutableArray alloc] initWithObjects:@"平面设计",@"大学英语",@"思想政治",@"色彩", nil];
    _allSamllArr = [[NSMutableArray alloc] initWithObjects:familyArr,friendArr,classArr, nil];
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 120, ScreenWidth, ScreenHeight - 70) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 40/h;
    [self.view addSubview:self.tableView];
    self.tableView.sectionHeaderHeight = 60/h;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
#pragma mark - TableView的代理协议
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sectionTitleArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *array = [_allSamllArr objectAtIndex:section];
    if (_flagArr[section] == YES)
    {
        return array.count;
    }
    else
    {
        return 0;
    }

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        //取消选中效果
        cell.selectionStyle = UITableViewCellAccessoryNone;
    }
        //
        NSMutableArray *array = [_allSamllArr objectAtIndex:indexPath.section];
        
        NSString *title = [array objectAtIndex:indexPath.row];
        
        cell.textLabel.text = title;
        cell.backgroundColor = [UIColor colorWithRed:231/255.0 green:235/255.0 blue:235/255.0 alpha:1];
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CGFloat h = 736 / ScreenHeight;
    CGFloat w = 414 / ScreenWidth;
    
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor lightGrayColor];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20/w, 10/h, 150/w, 40/h)];
    titleLabel.font = [UIFont systemFontOfSize:10/w];
    titleLabel.text = [_sectionTitleArr objectAtIndex:section];
    [redView addSubview:titleLabel];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, ScreenWidth, 60/h);
    button.tag = section;
    [button addTarget:self action:@selector(sectionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [redView addSubview:button];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 40/w, 10/h, 30/w, 40/h)];
    imageView.image = [UIImage imageNamed:@"下拉按钮图标-03"];
    [redView addSubview:imageView];
    
    //因为点击区头的时候,要重新刷新区,执行自定义的区头方法,可以把imageView的旋转动画方法放到自定义区头里面
    if (_flagArr[section] == YES)
    {
        //让imageView旋转90°
        imageView.transform = CGAffineTransformMakeRotation(M_PI);
    }
    else
    {
        //把图片旋转到原来的位置
        imageView.transform = CGAffineTransformIdentity;
    }
    
    return redView;
}
- (void)sectionBtnClick:(UIButton *)button
{
    //在区头按钮的点击方法中对bOOL数组进行取反的操作
    _flagArr[button.tag] = !_flagArr[button.tag];
    
    //在对BOOL数组取反之后, 要及时的刷新表
    //刷新整张表(所有的区全部刷新)
    //    [_tableView reloadData];
    
    
    //NSIndexSet:集合类
    
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:button.tag];
    
    //刷新一个区的方法
    [_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
    
}



/*---------*/
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        self.hidesBottomBarWhenPushed = YES;
        EvaluationViewController * VC = [[EvaluationViewController alloc]init];
        [self.navigationController pushViewController:VC animated:NO];

    }
}
/*---------*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
